import datetime
import json
import os

import pymysql
import requests
import time

from Base import Log, GetYamlFile
from Base.GetYamlFile import getYamlPath
from Base.HttpBase import HttpBase
from Base.ReadExcel import Read_Excel

log = Log.Log()
# base_url = 'http://192.168.17.236:30810'  # 独立租户2环境
base_url = 'http://192.168.17.236:30510'  # 独立租户1环境
tenant_id = 1  # 独立租户1环境
# base_url = 'http://111.20.218.54:30510'  # 凤县线上环境
# tenant_id =2561 # 凤县线上环境
login_api = base_url + '/upms-provider/tenant/user/login/mobile'
login_data = {
    'mobile': '18368093710',
    'password': 'Aa123456'
}
headers = {
    'Content-Type': 'application/json'
}

is_list = []

token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('access_token')
Refresh_token = HttpBase(login_api, login_data, 'post', headers).getHttpHeaders().get('Refresh_token')
log.info(token)
# 基础数据

# 基础请求头
base_headers = {
    'type': 'tenant',
    'tenant_id': tenant_id,
    'access_token': token,
    'Refresh_token': Refresh_token,
    # 'Version': 'middleground-2.0.5.6-SNAPSHOT'
}


def shoukuandan():
    """
收款单导入
    """
    add_headers = base_headers
    yml_list = GetYamlFile.getYamlFile()
    pricePath = yml_list["yyPath"]
    PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
    REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, pricePath)
    readBook = Read_Excel(REPORT_XLSX_DIR)
    num = readBook.get_RowNums("sheet")
    print(num)
    list1 = []
    for i in range(3, num):
        customer_code = readBook.read_Excel("sheet", i, 0)
        money = readBook.read_Excel("sheet", i, 3)
        project_customer_url = base_url + '/tenant-lms-basic-provide/projectCustomer/list'
        page_union_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/pageUnion'
        account_list = base_url + '/tenant-lms-account-provide/capitalAccount/page'

        customer_data = {
            'status': 0,
            'auditStatus': 1,
            'current': 1,
            'size': 10,
            'nameOrCode': customer_code
        }
        result = requests.get(page_union_url, customer_data, headers=add_headers)
        print('111' + str(result.content, 'utf-8'))
        print(customer_code)
        customer_name = json.loads(result.content).get("data").get('records')[0].get('name')
        customer_id = json.loads(result.content).get("data").get('records')[0].get('id')

        print('111222' + customer_id)
        list_data = {
            'current': 1,
            'size': 50,
            'type': 1,
            'tenantId': 2561,
            'customerId': customer_id
        }
        list_result = requests.post(account_list, params=list_data, headers=add_headers)
        account_list_id = json.loads(list_result.content).get("data").get('records')[0].get('id')
        # print(list_result)
        print(account_list_id)
        print(customer_code)
        print(money)
        date = {
            "tenantId": 2561,
            "tenantName": "凤县声威建材有限公司",
            "payTime": "2023-08-22 00:50:49",
            "customerId": customer_id,
            "customerName": customer_name,
            "checkStatus": 3,
            "receiptRecordQos": [{
                "customerId": customer_code,
                "summaryContentCode": "1",
                "businessTypeCode": "1",
                "payType": "7",
                "paymentMethodCode": "0",
                "accountId": account_list_id,
                "accountName": "通用账户",
                "money": money,
                "customerName": customer_name,
                "summaryContent": "水泥款",
                "businessTypeName": "水泥款",
                "businessType": "526",
                "paymentMethodName": "现金",
                "num": 1,
                "key": 0
            }],
            "businessRouteUrl": "/lms-web/#/receiptVoucherDetailH5?id="
        }
        account_save = base_url + '/tenant-lms-account-provide/capitalReceipt/save'
        account_save_result = requests.post(account_save, json=date, headers=add_headers)
        print(str(account_save_result.content, 'utf-8'))


def account_revise():
    account_detail_url = base_url + '/tenant-lms-account-provide/capitalReceipt/detail'
    account_revise_url = base_url + '/tenant-lms-account-provide/capitalReceipt/revise'
    add_headers = base_headers
    list2 = account_page()
    for item in range(0, len(list2)):

        account_detail_data = {
            'id': list2[item]
        }
        result = requests.get(account_detail_url, params=account_detail_data, headers=add_headers)
        # print(str(result.content, 'utf-8'))
        # print(json.loads(result.content).get("data"))
        account_revise_data = json.loads(result.content).get("data")
        if account_revise_data.get('customerName') in is_list:
            print("====不更新====" + account_revise_data.get('customerName'))
        else:
            account_revise_data = eval(str(account_revise_data))
            # print(type(account_revise_data))
            account_revise_data['receiptRecordQos'] = account_revise_data['receiptRecords']
            del account_revise_data['receiptRecords']
            # print(account_revise_data)
            account_revise_data['checkStatus'] = 0
            account_revise_data['receiptRecordQos'][0]['collectAccount'] = '1'
            account_revise_data['receiptRecordQos'][0]['paymentMethodName'] = '企业网银'
            account_revise_data['receiptRecordQos'][0]['paymentMethodCode'] = '2'
            print('======开始更新====' + account_revise_data.get('customerName'))
            # print(account_revise_data)
            result = requests.post(account_revise_url, json=account_revise_data, headers=add_headers)
            print(str(result.content, 'utf-8'))


def account_page():
    list1 = []
    account_page_url = base_url + '/tenant-lms-account-provide/capitalReceipt/page'
    account_page_data = {
        'current': 1,
        'size': 400,
        'checkStatus': 1
    }
    add_headers = base_headers
    result = requests.post(account_page_url, params=account_page_data, headers=add_headers)

    print(str(result.content, 'utf-8'))
    data1 = json.loads(result.content).get("data").get('records')
    print(len(data1))
    for item in range(0, len(data1)):
        list1.append(data1[item].get('id'))
    print(list1)
    return list1


def page_union():
    list1 =[]
    page_union_url = base_url + '/tenant-lms-basic-provide/lms/basic/customer/pageUnion'
    add_headers = base_headers
    customer_data = {
        'status': 0,
        'auditStatus': 1,
        'current': 1,
        'size': 9999,
        # 'nameOrCode': customer_code
    }
    result = requests.get(page_union_url, customer_data, headers=add_headers)
    print(str(result.content, 'utf-8'))

if __name__ == '__main__':
    # account_page()
    # data = {'a': 1, 'b': 2}
    # data['c'] = data.pop('a')
    # dict = {'a': 1, 'b': 2}
    # dict["c"] = dict.pop("a")
    # print(data)
    page_union()
